import random

# 创建系统功能菜单
menu = '''
********************************
*          中国工商银行           *
*          账户管理系统           *
*             V1.0             *
********************************
********************************
* 1.开户                        *
* 2.存钱                        *
* 3.取钱                        *
* 4.转账                        *
* 5.查询                        *
* 6.Bye!                       *
********************************
'''

# 银行信息
bank_name = "中国工商银行"
user_accounts = {}
account_limit = 100


# 开户_即添加用户信息
def add_user(name, password, country, province, street, door, balance):
    """
    添加用户
    :param door:
    :param country:
    :param province:
    :param street:
    :param name: 姓名
    :param password: 密码
    :param balance: 余额
    :return: 添加结果
    """
    global user_accounts, account
    global bank_name
    if len(user_accounts) >= account_limit:
        return 3  # 用户库已满
    account = str(random.randint(10000000, 99999999))
    while account in user_accounts:
        account = str(random.randint(10000000, 99999999))  # 如果该账户已经存在则重新生成
    user = {
        'account': account,
        'name': name,
        'password': password,
        'country': country,
        'province': province,
        'street': street,
        'door': door,
        'balance': balance,
        'bank_name': bank_name
    }
    user_accounts[account] = user
    return 1  # 添加成功


def deposit(account, amount):
    """
    存钱
    :param account: 账号
    :param amount: 存款金额
    :return: 存款结果
    """
    global user_accounts
    if account in user_accounts:
        user_accounts[account]['balance'] += amount
        return True
    return False


def withdraw(account, password, amount):
    """
    取钱
    :param account: 账号
    :param password: 密码
    :param amount: 取款金额
    :return: 取款结果
    """
    global user_accounts
    if account in user_accounts:
        if user_accounts[account]['password'] == password:
            if user_accounts[account]['balance'] >= amount:
                user_accounts[account]['balance'] -= amount
                return 0  # 取款成功
            return 3  # 余额不足
        return 2  # 密码错误
    return 1  # 账号不存在


def transfer(from_account, to_account, password, amount):
    """
    转账
    :param from_account: 转出账号
    :param to_account: 转入账号
    :param password: 转出账号密码
    :param amount: 转账金额
    :return: 转账结果
    """
    global user_accounts
    if from_account in user_accounts and to_account in user_accounts:
        if user_accounts[from_account]['password'] == password:
            if user_accounts[from_account]['balance'] >= amount:
                user_accounts[from_account]['balance'] -= amount
                user_accounts[to_account]['balance'] += amount
                return 0  # 转账成功
            return 3  # 余额不足
        return 2  # 密码错误
    return 1  # 账号不存在


# 查询账户信息
def query_account(account, password):
    """
    查询账户信息
    :param account: 账号
    :param password: 密码
    """
    global user_accounts
    if account in user_accounts:
        if user_accounts[account]['password'] == password:
            user = user_accounts[account]
            print(
                f"用户姓名：{user['name']},当前账号：{user['account']}, 密码:{user['password']},"
                f"余额：{user['balance']}元，用户居住地址：国家：{user['country']} 省份：{user['province']} 街道：{user['street']} "
                f"门牌：{user['door']}号，当前账户的开户行：{user['bank_name']}{user['province']}分行{user['street']}支行")
        else:
            print("密码不正确.")
    else:
        print("该用户不存在.")


while True:
    print(menu)
    choice = input("请选择业务功能：")

    if choice == "1":
        # 开户
        name = str(input("请输入姓名："))
        password = int(input("请输入密码："))
        country = str(input("请输入国家："))
        province = str(input("请输入省份："))
        street = str(input("请输入街道："))
        door = str(input("请输入门牌："))
        balance = int(input("请输入存款余额："))
        result = add_user(name, password, country, province, street, door, balance)
        if result == 1:
            print(f"开户成功，您的账号为：{user_accounts[account]['account']}")
        elif result == 3:
            print("用户库已满.")

    elif choice == "2":
        # 存钱
        account = input("请输入账号：")
        amount = int(input("请输入存款金额："))
        if deposit(account, amount):
            print("存钱成功.")
        else:
            print("账号不存在.")

    elif choice == "3":
        # 取钱
        account = input("请输入账号：")
        password = int(input("请输入密码："))
        amount = int(input("请输入取款金额："))
        result = withdraw(account, password, amount)
        if result == 0:
            print("取钱成功.")
        elif result == 1:
            print("账号不存在.")
        elif result == 2:
            print("密码不正确.")
        elif result == 3:
            print("余额不足.")

    elif choice == "4":
        # 转账
        from_account = input("请输入转出账号：")
        to_account = input("请输入转入账号：")
        password = int(input("请输入转出账号的密码："))
        amount = int(input("请输入转出金额："))
        result = transfer(from_account, to_account, password, amount)
        if result == 0:
            print("转账成功.")
        elif result == 1:
            print("账号不对.")
        elif result == 2:
            print("密码不对.")
        elif result == 3:
            print("余额不足.")

    elif choice == "5":
        # 查询账户信息
        account = input("请输入账号：")
        password = int(input("请输入密码："))
        query_account(account, password)

    elif choice == "6":
        print("Bye!")
        break

    else:
        print("请选择正确的业务功能.")

